package com.qf.petservice_api.util;

import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    /**
     * 计算2个日期相差的天数*/
    public static int diffDay(Date date1){
        //现在
        Calendar calendar1=Calendar.getInstance();
        //曾经
        Calendar calendar2=Calendar.getInstance();
        calendar2.setTime(date1);
        int year1=calendar1.get(Calendar.YEAR);
        int year2=calendar2.get(Calendar.YEAR);
        int day=calendar1.get(Calendar.DAY_OF_YEAR)-calendar2.get(Calendar.DAY_OF_YEAR);
        if(year1!=year2){
            //不是同一年
            for(int i = year1 ; i < year2 ; i ++)
            {
                //验证是否为闰年  闰年366天
                if(i%4==0 && i%100!=0 || i%400==0){
                    day += 366;
                }else{
                    //平年，365
                    day += 365;
                }
            }
        }
        return day;
    }
}
